package com.qyhl.party.party.study.subject.single;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.party.R;
import com.qyhl.party.R2;
import com.qyhl.party.party.study.subject.PartySubjectContract;
import com.qyhl.party.party.study.subject.PartySubjectPresenter;
import com.qyhl.party.party.study.subject.item.PartyNewsLargeItemDelegate;
import com.qyhl.party.party.study.subject.item.PartyNewsLongItemDelegate;
import com.qyhl.party.party.study.subject.item.PartyNewsNormalItemDelegate;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.news.NewsBean;
import com.qyhl.webtv.commonlib.entity.news.UnionBean;
import com.qyhl.webtv.commonlib.utils.ResizableImageView;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.PARTY_SUBJECT_SINGLE)
public class PartySubSingleActivity extends BaseActivity implements PartySubjectContract.PartySubjectView {
    @BindView(R2.id.catalog_recycler)
    RecyclerView recyclerView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    @Autowired(name = "title")
    String titleName;
    @Autowired(name = "logo")
    String logo;
    @Autowired(name = "InnerCode")
    String InnerCode;

    RecyclerView subRecycler;
    TextView title;
    ResizableImageView cover;

    private PartySubjectPresenter mPresenter;

    private List<NewsBean> mDataList = new ArrayList<>();

    private MultiItemTypeAdapter<NewsBean> newsAdapter;

    private int ID = 0;

    private HeaderAndFooterWrapper headerAndFooterWrapper;

    @Override
    protected int getContentView() {
        return R.layout.party_activity_subject;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar.statusBarColor(R.color.white).statusBarDarkFont(true).fitsSystemWindows(true).init();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        loadMask.setStatus(LoadingLayout.Loading);

        mPresenter = new PartySubjectPresenter(this);

        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setEnableLoadMore(true);


        View headView = LayoutInflater.from(this).inflate(R.layout.party_header_subject, null);
        subRecycler = headView.findViewById(R.id.sub_recycler);
        title = headView.findViewById(R.id.title);
        cover = headView.findViewById(R.id.cover);

        subRecycler.setVisibility(View.GONE);

        title.setText(titleName);

        Glide.with(this)
                .load(logo)
                .apply(new RequestOptions().error(R.drawable.cover_large_default).placeholder(R.drawable.cover_large_default))
                .into(cover);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        //初始化适配器
        newsAdapter = new MultiItemTypeAdapter<NewsBean>(this, mDataList);
        newsAdapter.addItemViewDelegate(new PartyNewsLargeItemDelegate(this));
        newsAdapter.addItemViewDelegate(new PartyNewsLongItemDelegate(this));
        newsAdapter.addItemViewDelegate(new PartyNewsNormalItemDelegate(this));

        newsAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                NewsBean newsBean = mDataList.get(i - 1);
                switch (newsBean.getType()) {
                    case "1"://普通新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", newsBean.getID());
                        RouterManager.router(ARouterPathConstant.NORMAL_NEWS_ACTIVITY, bundle);
                        ActionLogUtils.getInstance().onEvent(ActionConstant.PARTY_STUDY_NEWS);
                    }
                    break;
                    case "2"://图片新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", newsBean.getID());
                        bundle.putString("section", newsBean.getCatalogID());
                        RouterManager.router(ARouterPathConstant.PICTURE_NEWS_ACTIVITY, bundle);
                        ActionLogUtils.getInstance().onEvent(ActionConstant.PARTY_STUDY_NEWS);
                    }

                    break;
                    case "3"://直播
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("newsId", newsBean.getID());
                        bundle.putString("liveType", newsBean.getLivetype());
                        RouterManager.router(ARouterPathConstant.LIVE_NORMAL_ACTIVITY, bundle);
                        ActionLogUtils.getInstance().onEvent(ActionConstant.PARTY_STUDY_LIVE);
                    }
                    break;
                    case "4"://标题新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", newsBean.getID());
                        bundle.putString("title", newsBean.getTitle());
                        bundle.putString("url", newsBean.getRedirectURL());
                        RouterManager.router(ARouterPathConstant.ADV, bundle);
                        ActionLogUtils.getInstance().onEvent(ActionConstant.PARTY_STUDY_NEWS);
                    }
                    break;
                    case "5"://视频新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", newsBean.getID());
                        RouterManager.router(ARouterPathConstant.VIDEO_NEWS_ACTIVITY, bundle);
                        ActionLogUtils.getInstance().onEvent(ActionConstant.PARTY_STUDY_LIVE);
                    }
                    break;
                    case "8"://互动直播
                        break;
                    case "101"://抓取新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", newsBean.getID());
                        RouterManager.router(ARouterPathConstant.NEWS_CATCH, bundle);
                        ActionLogUtils.getInstance().onEvent(ActionConstant.PARTY_STUDY_NEWS);
                    }
                    break;
                    default:
                        break;

                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                return false;
            }
        });


        headerAndFooterWrapper = new HeaderAndFooterWrapper<NewsBean>(newsAdapter);
        headerAndFooterWrapper.addHeaderView(headView);
        recyclerView.setAdapter(headerAndFooterWrapper);

        mPresenter.getList(InnerCode, ID + "");
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                ID = 0;
                mPresenter.getList(InnerCode, ID + "");
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                ID = 0;
                mPresenter.getList(InnerCode, ID + "");
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getList(InnerCode, ID + "");
            }
        });
    }

    @OnClick(R2.id.back_btn)
    public void onClick() {
        finish();
    }

    @Override
    public void setSub(List<UnionBean> beanList) {

    }

    @Override
    public void setList(List<NewsBean> beanList) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        if (beanList != null && beanList.size() > 0) {
            if (ID == 0) {
                refresh.finishRefresh();
                mDataList.clear();
            } else {
                refresh.finishLoadMore();
            }
            mDataList.addAll(beanList);
            ID = Integer.parseInt(beanList.get(beanList.size() - 1).getID());
            headerAndFooterWrapper.notifyDataSetChanged();
        } else {
            setError("暂无跟多内容");

        }
    }

    @Override
    public void setError(String msg) {
        if (ID == 0) {
            refresh.finishRefresh();
            loadMask.setStatus(LoadingLayout.Error);
            loadMask.setErrorText(msg);
            loadMask.setReloadButtonText("点击重试~");
        } else {
            refresh.finishLoadMore();
            showToast(msg);
        }
    }
}
